﻿using System.Windows;
using System.ComponentModel;
using System.Threading;
using System.Net;
using System.Threading.Tasks;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        BackgroundWorker bgWorker = new BackgroundWorker();
        //线程状态对象，这里是关键  
        private ManualResetEvent manualReset = new ManualResetEvent(true);  

        public MainWindow()
        {
            InitializeComponent();
            bgWorker.WorkerReportsProgress = true;
            bgWorker.WorkerSupportsCancellation = true;
            bgWorker.DoWork += DoWork_Handler;
            bgWorker.ProgressChanged += ProgressChanged_Handler;
            bgWorker.RunWorkerCompleted += RunWorkerCompleted_Handler;
        }

        private void ProgressChanged_Handler(object sender, ProgressChangedEventArgs e)
        {
            //修改进度条的显示
            progressBar.Value = e.ProgressPercentage;
            //txtProgress.Text =e.ProgressPercentage.ToString ();
            txtProgress.Text = e.UserState.ToString();  //come from ReportProgress()
        }

        private void RunWorkerCompleted_Handler(object sender, RunWorkerCompletedEventArgs e)
        {
            progressBar.Value = 0;
            if (e.Cancelled)
            {
                MessageBox.Show("后台任务已经被取消。", "消息");
            }
            else
            {
                MessageBox.Show("后台任务正常结束。", "消息");
            }
        }

        private void DoWork_Handler(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            for (int i = 1; i <= 50; i++)
            {
                // 判断用户是否要求取消后台操作，以尽早退出
                if (worker.CancellationPending)
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                    string message = "Current sum is: " + i.ToString();
                    //ReportProgress 方法把信息传递给 ProcessChanged 事件处理函数。
                    //第一个参数类型为 int，表示执行进度。
                    //第二个参数用于传递用户自定义消息。
                    worker.ReportProgress(i * 2,message);
                    Thread.Sleep(500);
                }
                manualReset.WaitOne();
            }
        }

        private void btnProcess_Click(object sender, RoutedEventArgs e)
        {
            if (!bgWorker.IsBusy)
            {
                bgWorker.RunWorkerAsync();  //开始执行后台操作，引发 DoWork 事件
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            //取消后台操作,并不马上终止后台操作
            bgWorker.CancelAsync();  
        }

        private void btnPause_Click(object sender, RoutedEventArgs e)
        {
            if (btnPause.Content == "暂停")
            {
                //线程状态对象，这里是关键  
                manualReset.Reset();//暂停当前线程的工作，发信号给waitOne方法，阻塞  
                btnPause.Content = "继续";
            }
            else
            {
                //线程状态对象，这里是关键  
                manualReset.Set();//继续某个线程的工作  
                btnPause.Content = "暂停";
            }  
        }
   
    }
}
